package com.zretc.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BookController {

	@PostMapping("book")
	public ModelAndView addBook(String title,@RequestParam("file") Part file,HttpServletRequest request) throws IOException 
	{
		ModelAndView mv = new ModelAndView("showbook");
		System.out.println("title :"+title);
		System.out.println(request.getRemoteAddr());
		System.out.println("realpath :"+request.getRealPath("\\imgs"));
		String path = request.getRealPath("\\imgs");
		//使用file的方法完成上传的操作,写个固定地址，不科学 ;file.getSubmittedFileName() 文件名
		file.write(path+"\\"+file.getSubmittedFileName());
		
		mv.addObject("title", title);
		mv.addObject("imgpath",file.getSubmittedFileName());
		//转发页面，显示图片
		return mv;
	}
}
